/**
 * 统一返回值中间件
 */

const response_mode = {
  fail: { code: 0, msg: "请求繁忙" },
  success: { code: 1, msg: "请求成功" },
  auth: { code: 2, msg: "无效token" },
};

module.exports = (req, res, next) => {
  res.backSuccess = (data) =>
    res.status(200).json({ ...response_mode.success, data });

  res.backFail = (data, msg) =>
    res.status(200).json({
      ...response_mode.fail,
      data,
      msg: msg ?? response_mode.fail.msg,
    });

  res.backAuthFail = (msg) =>
    res.status(200).json({
      ...response_mode.auth,
      msg: msg ?? response_mode.auth.msg,
    });

  next();
};
